package Game.Spawners;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.SlickException;

import Game.GameHelper;
import Game.Level;
import Game.Objects.GOAltar;
import Game.Objects.GOSeller;
import Game.Objects.GameObject;
import GameMath.GPoint;

/** Spawn Seller class
 * @author Aikidos
 */
public class SpawnSeller extends SpawnerBase {

	@Override
	public void spawn(Level level) throws SlickException {
		Random rnd = GameHelper.getRandom();
		int numOfSellers = level.isUndergroundTown() ? rnd.nextInt(3) + 1 :
			(rnd.nextInt(2) == 1) ? 1 : 0;
		
		if (numOfSellers > 0) {
			List<GPoint> banCoords = new ArrayList<GPoint>();
			banCoords.add(level.getObjectManager().getPlayer().getCoord());
			
			if (!level.isUndergroundTown()) {
				banCoords.add(level.getObjectManager().getObjectsByClass(
						"Game.Objects.GOLever")[0].getCoord());
			}
			
			banCoords.add(level.getObjectManager().getObjectsByClass(
					"Game.Objects.GOTrapdoor")[0].getCoord());
			
			// Doors
			GameObject[] doors = level.getObjectManager().getObjectsByClass(
					"Game.Objects.GODoor");
			for (GameObject door : doors) {
				banCoords.add(door.getCoord());
			}

			// Keys
			GameObject[] keys = level.getObjectManager().getObjectsByClass(
					"Game.Objects.GODoorKey");
			for (GameObject key : keys) {
				banCoords.add(key.getCoord());
			}

			// Chests
			GameObject[] chests = level.getObjectManager().getObjectsByClass(
					"Game.Objects.GOGoldChest");
			for (GameObject chest : chests) {
				banCoords.add(chest.getCoord());
			}

			// Torches
			GameObject[] torches = level.getObjectManager().getObjectsByClass(
					"Game.Objects.GOTorch");
			for (GameObject torch : torches) {
				banCoords.add(torch.getCoord());
			}

			// Barrels
			GameObject[] barrels = level.getObjectManager().getObjectsByClass(
					"Game.Objects.GOBarrel");
			for (GameObject barrel : barrels) {
				banCoords.add(barrel.getCoord());
			}
			
			for (int i = 0; i < numOfSellers; i++) {
				GOSeller newSeller = new GOSeller(level.getObjectManager());
				
				level.getObjectManager().spawnObject(newSeller, banCoords);
				
				banCoords.add(newSeller.getCoord());
			}
		}
	}
	
}
